using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Vms.Web.Models;
using Vms.Web.Lib;

namespace Vms.Web.Areas.Lib.Controllers
{   
    public class ContentController : Controller
    {
        private VmsWebContext context = new VmsWebContext();

        //
        // GET: /LIB_CONTENT/

        public ActionResult Index(int contentId = -1)
        {
            List<LIB_CONTENT> lib_contents = context.LIB_CONTENTS.ToList();
            ViewBag.Lib_Contents = lib_contents;
          
            LIB_CONTENT lib_content = new LIB_CONTENT();


            #region INIT_DROP_DOWN_LIST_DATA

            // Font_Color
            List<SelectListItem> fontColors = new List<SelectListItem>();
            for (int i = 0; i < Utility.FONT_COLOR.GetLength(0); ++i)
            {
                SelectListItem item = new SelectListItem();
                item.Text = (string)Utility.FONT_COLOR[i, 0];
                item.Value = "" + Utility.FONT_COLOR[i, 1];
                fontColors.Add(item);
            }
            ViewBag.FontColors = fontColors;

            // Font_Name
            List<SelectListItem> fontNames = new List<SelectListItem>();
            for (int i = 0; i < Utility.FONT_NAME.Length; ++i)
            {
                SelectListItem item = new SelectListItem();
                item.Text = Utility.FONT_NAME[i];
                item.Value = Utility.FONT_NAME[i];
                fontNames.Add(item);
            }
            ViewBag.FontNames = fontNames;

            // Display Mode
            var displayMode = new List<object>();
            for (int i = 0; i < Utility.DISPLAY_MODE.GetLength(0); ++i)
            {
                var item = new
                {
                    Text = Utility.DISPLAY_MODE[i, 0],
                    Value = Utility.DISPLAY_MODE[i, 1]
                };
                displayMode.Add(item);
            }
            ViewBag.DisplayMode = displayMode;

            // Speeds
            var speeds = new List<object>();
            for (int i = 0; i < 64; ++i)
            {
                var item = new
                {
                    Text = i,
                    Value = i
                };
                speeds.Add(item);
            }
            ViewBag.Speeds = speeds;

            #endregion 

            if (contentId != -1)   // Edit Content
            {
                lib_content = context.LIB_CONTENTS.Single(p => p.Id == contentId);
                ViewBag.contentId = lib_content.Id;
            }
            else // Create a new content
            {
                LIB_CONTENT_IMG img = new LIB_CONTENT_IMG();
                img.Img_Height = img.Img_Width = img.Img_x = img.Img_y = 0;
                LIB_CONTENT_TXT txt = new LIB_CONTENT_TXT();
                txt.Txt_Height = txt.Txt_Width = txt.Txt_x = txt.Txt_y = 0;
                txt.FontSize = 14;
                lib_content.LIB_CONTENT_IMG.Add(img);
                lib_content.LIB_CONTENT_TXT.Add(txt);
            }
            return View(lib_content);
        }

        //
        // GET: /LIB_CONTENT/Details/5

        public ViewResult Details(int id)
        {
            LIB_CONTENT lib_content = context.LIB_CONTENTS.Single(x => x.Id == id);
            return View(lib_content);
        }

        [HttpPost]
        public ActionResult EditContent(FormCollection collection)
        {
            if (ModelState.IsValid)
            {
                // Update content
                int id = int.Parse(collection["Content_ID"]);
                LIB_CONTENT content = context.LIB_CONTENTS.Single(c => c.Id == id);
                context.Entry(content).State = EntityState.Modified;
                content.Name = collection["Content_Name"];
                // Update Content Img
                int img_id = int.Parse(collection["Img_ID"]);
                LIB_CONTENT_IMG content_img = content.LIB_CONTENT_IMG.Single(img => img.Id == img_id);
                context.Entry(content_img).State = EntityState.Modified;

                content_img.Img_Height = int.Parse(collection["Img_Height"]);
                content_img.Img_Width = int.Parse(collection["Img_Width"]);
                content_img.Img_x = int.Parse(collection["Img_x"]);
                content_img.Img_y = int.Parse(collection["Img_y"]);
                content_img.RunSpeed = int.Parse(collection["Img_RunSpeed"]);
                content_img.DisplayMode = int.Parse(collection["Img_DisplayMode"]);
                content_img.ImgFileName = collection["ImgFileName"];

                // Update Content txt
                int txt_id = int.Parse(collection["Txt_ID"]);
                LIB_CONTENT_TXT content_txt = content.LIB_CONTENT_TXT.Single(txt => txt.Id == txt_id);
                context.Entry(content_txt).State = EntityState.Modified;

                content_txt.Txt = collection["Txt"];
                content_txt.Txt_Height = int.Parse(collection["Txt_Height"]);
                content_txt.Txt_Width = int.Parse(collection["Txt_Width"]);
                content_txt.Txt_x = int.Parse(collection["Txt_x"]);
                content_txt.Txt_y = int.Parse(collection["Txt_y"]);
                content_txt.RunSpeed = int.Parse(collection["Txt_RunSpeed"]);
                content_txt.DisplayMode = int.Parse(collection["Txt_DisplayMode"]);

                content_txt.FontName = collection["FontName"];
                content_txt.FontSize = int.Parse(collection["FontSize"]);
                content_txt.FontColor = int.Parse(collection["FontColor"]);
                content_txt.Bold = ("true".Equals(collection["Bold"])) ? true : false;

                string file = Utility.pathLibContentTxt + content_txt.TxtFileName;

                if (System.IO.File.Exists(file))
                {
                    System.IO.StreamWriter sw = System.IO.File.CreateText(file);
                    sw.Write(content_txt.Txt);
                    sw.Close();
                }
                else
                {
                    file = "txt_" + DateTime.Now.Ticks + ".txt";
                    System.IO.File.CreateText(Utility.pathLibContentTxt + file);
                    content_txt.TxtFileName = file;
                }

                // Save update content
                context.SaveChanges();
                return RedirectToAction("Index");
            }
            return View();
        }

        //
        // POST: /LIB_CONTENT/Create

        [HttpPost]
        public ActionResult CreateContent(FormCollection collection)
        {
            if (ModelState.IsValid)
            {
                // Create new content
                LIB_CONTENT content = new LIB_CONTENT();
                content.Name = collection["Content_Name"];

                // Update Content Img
                LIB_CONTENT_IMG content_img = new LIB_CONTENT_IMG();

                content_img.Img_Height = int.Parse(collection["Img_Height"]);
                content_img.Img_Width = int.Parse(collection["Img_Width"]);
                content_img.Img_x = int.Parse(collection["Img_x"]);
                content_img.Img_y = int.Parse(collection["Img_y"]);
                content_img.RunSpeed = int.Parse(collection["Img_RunSpeed"]);
                content_img.DisplayMode = int.Parse(collection["Img_DisplayMode"]);
                content_img.ImgFileName = collection["ImgFileName"];

                // Update Content txt
                LIB_CONTENT_TXT content_txt = new LIB_CONTENT_TXT();

                content_txt.Txt = collection["Txt"];
                content_txt.Txt_Height = int.Parse(collection["Txt_Height"]);
                content_txt.Txt_Width = int.Parse(collection["Txt_Width"]);
                content_txt.Txt_x = int.Parse(collection["Txt_x"]);
                content_txt.Txt_y = int.Parse(collection["Txt_y"]);
                content_txt.RunSpeed = int.Parse(collection["Txt_RunSpeed"]);
                content_txt.DisplayMode = int.Parse(collection["Txt_DisplayMode"]);

                content_txt.FontName = collection["FontName"];
                content_txt.FontSize = int.Parse(collection["FontSize"]);
                content_txt.FontColor = int.Parse(collection["FontColor"]);
                content_txt.Bold = ("true".Equals(collection["Bold"])) ? true : false;

                string file = Utility.pathLibContentTxt + content_txt.TxtFileName;

                if (System.IO.File.Exists(file))
                {
                    System.IO.StreamWriter sw = System.IO.File.CreateText(file);
                    sw.Write(content_txt.Txt);
                    sw.Close();
                }
                else
                {
                    file = "txt_" + DateTime.Now.Ticks + ".txt";
                    System.IO.StreamWriter sw = System.IO.File.CreateText(file);
                    sw.Write(content_txt.Txt);
                    sw.Close();
                    content_txt.TxtFileName = file;
                }

                // Save create content
                content.LIB_CONTENT_IMG.Add(content_img);
                content.LIB_CONTENT_TXT.Add(content_txt);
                context.LIB_CONTENTS.Add(content);
                context.SaveChanges();
            }
            return View();
        }
        
        //
        // POST: /LIB_CONTENT/Edit/5

        [HttpPost]
        public ActionResult Edit(LIB_CONTENT lib_content)
        {
            if (ModelState.IsValid)
            {
                context.Entry(lib_content).State = EntityState.Modified;
                context.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(lib_content);
        }

        //
        // GET: /LIB_CONTENT/Delete/5

        public ActionResult Delete(int contentId)
        {
            LIB_CONTENT lib_content = context.LIB_CONTENTS.Single(x => x.Id == contentId);
            context.LIB_CONTENTS.Remove(lib_content);
            context.SaveChanges();
            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult EditImage(LIB_CONTENT_IMG content_img)
        {
            if (ModelState.IsValid)
            {
                content_img.DisplayMode = int.Parse(Request.Form.GetValues("DisplayMode")[0]);
                context.Entry(content_img).State = EntityState.Modified;

                context.SaveChanges();
                return RedirectToAction("Index");
            }
            return RedirectToAction("Index", "Home");
        }

        [HttpPost]
        public ActionResult EditText(LIB_CONTENT_TXT content_txt)
        {
            if (ModelState.IsValid)
            {
                context.Entry(content_txt).State = EntityState.Modified;
                string file = Utility.pathLibContentTxt + content_txt.TxtFileName;

                if (System.IO.File.Exists(file))
                {
                    System.IO.StreamWriter sw = System.IO.File.CreateText(file);
                    sw.Write(content_txt.Txt);
                    sw.Close();
                }
                else
                {
                    file = "txt_" + DateTime.Now.Ticks + ".txt";
                    System.IO.File.CreateText(Utility.pathLibContentTxt + file);
                    content_txt.TxtFileName = file;
                }
                content_txt.DisplayMode = int.Parse(Request.Form.GetValues("DisplayMode")[0]);
                context.SaveChanges();
                return RedirectToAction("Index");
            }
            return RedirectToAction("Index", "Home");
        }


        protected override void Dispose(bool disposing)
        {
            if (disposing) {
                context.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}